package com.itheima.health.controller.user;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dto.OrderSettingParam;
import com.itheima.health.dto.OrderSubmitParam;
import com.itheima.health.dto.VeiflcationcodeParam;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.result.Result;
import com.itheima.health.service.OrderSubmitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/mobile/order")
@Slf4j
public class OrderSubmitController {
    @Autowired
    private OrderSubmitService orderSubmitService;
    /*
    * 套餐预约功能
    * */
    @PostMapping("/submit")
    public Result ordersubmit(@RequestBody OrderSubmitParam orderSubmitParam){
        /*
        * 新增预约
        * */
        //先查询数据库根据身份证号和预约日期来判断是否重复预约
       Integer count = orderSubmitService.selectrepeat(orderSubmitParam.getOrderDate(),orderSubmitParam.getIdCard());
       if (count == 0){
           //根据手机号查询验证码是否正确
           VeiflcationcodeParam v = orderSubmitService.selectCode(orderSubmitParam.getTelephone());
           //判断客户输入的验证码是否于数据库里的一致
           if (orderSubmitParam.getValidateCode().equals(v.getValidateCode())){
               orderSubmitService.addordersubmit(orderSubmitParam);
           }else {
               //不一致抛出验证码不正确异常
               throw new BusinessRuntimeException(MessageConst.VALIDATECODE_ERROR);
           }
       }else {
           throw new BusinessRuntimeException(MessageConst.HAS_ORDERED);
       }


        return new Result(true, MessageConst.ORDER_SUCCESS);
    }

    //TODO 查询预约详情未写
}
